﻿using System.Reflection;
using NLite.Reflection.Internal;
using NLite.Collections;

namespace NLite.Reflection
{
    
    /// <summary>
    /// 
    /// </summary>
    public static class CounstructorExtensions
    {
        private static readonly IMap<ConstructorInfo, ConstructorHandler> factoryMethodCache = new Map<ConstructorInfo, ConstructorHandler>();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="constructor"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static object FastInvoke(this ConstructorInfo constructor, params object[] args)
        {
            if (args == null) args = new object[0];
            return factoryMethodCache.GetOrAdd(constructor, ()=>constructor.GetCreator())(args);
        }
    }

}
